/**
 * loader frame
 * Copyright (c) 2009 cmiscm.com
 * date of final modification : 2009. 07. 13 
*/
package com.cmiscm.net.frame
{
	import com.cmiscm.net.events.NetEvent;
	
	import flash.events.EventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.events.SecurityErrorEvent;

	public class LoaderFrame extends EventDispatcher
	{
		protected var _isLoad:Boolean = false;
		protected var _total:Number = 0;
		
		public static const NO_FILE:String = "no_file";
		public static const TOTAL_ZERO:String = "total_zero";
		public static const PARSING_FAIL:String = "parsing_fail";
		public static const SECURITY_ERROR:String = "security_error";
		public static const COMPLETE_ERROR:String = "complete_error";
		public static const LOAD_ERROR:String = "load_error";
		
		public function LoaderFrame()
		{
			super();
		}
		
		protected function onProgress(evt:ProgressEvent):void
		{
			var loaded:Number = evt.bytesLoaded;
			_total = evt.bytesTotal;
			var percent:Number = loaded / _total;
			this.dispatchEvent(new NetEvent(NetEvent.PROGRESS, null, percent));
		}
		protected function onError(evt:IOErrorEvent):void
		{
			setError(NO_FILE);
		}
		protected function onSecurityError(evt:SecurityErrorEvent):void
		{
			setError(SECURITY_ERROR);
		}
		protected function checkTotal():void
		{
			if (_total == 0) this.dispatchEvent(new NetEvent(NetEvent.SERVER_ERROR, TOTAL_ZERO));
		}
		protected function setError($msg:String):void
		{
			_isLoad = false;
			this.dispatchEvent(new NetEvent(NetEvent.ERROR, $msg));
		}
		
	}
}